//
//  ClassRoomViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/11/6.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "ClassRoomViewController.h"
#import "ClassRoomTableViewCell.h"
#import "LiveClassRoomTableViewCell.h"
#import "LiveDetailViewController.h"
#import "ClassRoomDetailViewController.h"
#import "LiveListViewController.h"
#import "ChangeChoiceView.h"
#import "ClassRoomListViewController.h"
#import "YSSearchViewControllerViewController.h"

@interface ClassRoomViewController ()<UIScrollViewDelegate>
@property(nonatomic,weak) UIScrollView *scrollView;

@property(nonatomic,weak) ChangeChoiceView *choiceView;

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *classDataArray;
@property (nonatomic,strong) NSMutableArray *liveDataArray;
@property (nonatomic,strong) NSMutableArray *subjectList;
@end

@implementation ClassRoomViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)classDataArray{
    if (!_classDataArray) {
        _classDataArray = [NSMutableArray array];
    }
    return _classDataArray;
}
-(NSMutableArray *)liveDataArray{
    if (!_liveDataArray) {
        _liveDataArray = [NSMutableArray array];
    }
    return _liveDataArray;
}
- (NSMutableArray *)subjectList{
    if (!_subjectList) {
        _subjectList = [NSMutableArray array];
    }
    return _subjectList;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
//    [Global setStatusBarBackgroundColor:[UIColor clearColor]];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithImage:[UIImage imageNamed:@"search_icon"] highlighted:[UIImage imageNamed:@"search_icon"] target:self selector:@selector(searchClassRoom)];
    self.view.backgroundColor = KBACKGROUNDCOLOR;
    
    [self creatSubjectList];
}
#pragma mark - 搜索课堂
- (void)searchClassRoom{
    YSSearchViewControllerViewController *searchViewController = [[YSSearchViewControllerViewController alloc]init];
    searchViewController.searchType = KSearchClassType;
    [self.navigationController pushViewController:searchViewController animated:YES];
}

#pragma mark - 获取科目列表
- (void)creatSubjectList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppCourseGetSubjectList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [self.subjectList removeAllObjects];
            
            NSArray *array = [NSArray arrayWithArray:response];
            NSMutableArray *subjectIdArray = [NSMutableArray array];
            [self.subjectList addObject:@"直播"];
            [subjectIdArray addObject:@""];
            for (int i = 0; i < array.count; i++) {
                [self.subjectList addObject:array[i][@"name"]];
                [subjectIdArray addObject:array[i][@"id"] ];
            }
            double subjectWidth=0.0;
            for (int i =0; i < self.subjectList.count ; i ++) {
                subjectWidth = subjectWidth + [Global getSizeOfString:self.subjectList[i] maxWidth:MAXFLOAT maxHeight:20 withFontSize:16.6].width + 10;
            }
            
            ChangeChoiceView *choiceview=[[ChangeChoiceView alloc]initWithFrame:CGRectMake(0, 0, subjectWidth+10, 41) andtitleArray:self.subjectList];
            [self.view addSubview:choiceview];
            self.choiceView=choiceview;
            
            UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(choiceview.frame), kScreenW, kScreenH-CGRectGetMaxY(choiceview.frame))];
            scrollView.pagingEnabled=YES;
            scrollView.backgroundColor = [UIColor whiteColor];
            scrollView.delegate=self;
            scrollView.contentSize=CGSizeMake(CGRectGetWidth(scrollView.frame)*self.subjectList.count, 0);
            [self.view addSubview:scrollView];
            self.scrollView=scrollView;
            
            choiceview.clickbuttonblock=^(NSInteger index){
                CGPoint point=self.scrollView.contentOffset;
                self.scrollView.contentOffset=CGPointMake(index*CGRectGetWidth(self.scrollView.frame), point.y);
            }; 
            for (int i = 0 ; i < self.subjectList.count; i ++) {
                if (!i) {
                    LiveListViewController *liveListVC = [[LiveListViewController alloc]init];
                    liveListVC.subjectIDStr = subjectIdArray[i];
                    [liveListVC willMoveToParentViewController:self];
                    CGRect rect=scrollView.bounds;
                    rect.origin.x = kScreenW * i;
                    liveListVC.view.frame=rect;
                    [self addChildViewController:liveListVC];
                    [liveListVC didMoveToParentViewController:self];
                    [scrollView addSubview:liveListVC.view];
                }else{
                    ClassRoomListViewController *inVC=[[ClassRoomListViewController alloc]init];
                    inVC.subjectIDStr = subjectIdArray[i];
                    [inVC willMoveToParentViewController:self];
                    CGRect rect=scrollView.bounds;
                    rect.origin.x = kScreenW * i;
                    inVC.view.frame=rect;
                    [self addChildViewController:inVC];
                    [inVC didMoveToParentViewController:self];
                    [scrollView addSubview:inVC.view];
                }
                
            }
        }
    }];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollView) {
        NSInteger index=(NSInteger)(scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame)+0.5);
        [self.choiceView clickbuttonAtIndex:index];
    }
    
}
//#pragma mark - 获取直播列表
//- (void)creatLiveDataList{
//    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
//    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
//    [dic setValue:@"1" forKey:@"page"];   //页数 默认1
//    [dic setValue:@"1" forKey:@"rows"];   //每页数量
//    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
//    [dic setValue:@"1" forKey:@"videoType"];    //课程类型 1直播 2课程
//    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppCourseGetCourseList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
//        if (isuccess) {
//            NSArray *array = [NSArray arrayWithArray:response];
//            NSArray *liveArray = [NSArray arrayWithArray:array[0][@"courseList"][@"list"]];
//            for (int i = 0; i < liveArray.count; i++) {
//                [self.liveDataArray addObject:[ClassRoomInfo mj_objectWithKeyValues:liveArray[i]]];
//
//            }
////            [self creatHeadView];
//        }
//    }];
//}

//#pragma mark - 获取课程列表
//- (void)creatDataList{
//    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
//    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
////    [dic setValue:[NSString stringWithFormat:@"%ld",(long)self.pageNo] forKey:@"page"]; //分页
////    [dic setValue:KREFRESHROWS forKey:@"rows"];   //每页数量
//    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
//    [dic setValue:@"2" forKey:@"videoType"];    //课程类型 1直播 2课程
//    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppCourseGetCourseList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
//        if (isuccess) {
//            if (self.pageNo == 1)
//            {
//                [self.dataArray removeAllObjects];
//            }
//            NSArray *array = [NSArray arrayWithArray:response];
//            self.dataArray = [NSMutableArray arrayWithArray:response];
//            for (int i = 0; i < array.count; i++) {
//                NSArray *classArray = array[i][@"courseList"][@"list"];
//                NSMutableArray *curArray = [NSMutableArray array];
//                for (int j = 0 ; j < classArray.count; j ++) {
//                     [curArray addObject:[ClassRoomInfo mj_objectWithKeyValues:classArray[j]]];
//                }
//                [self.classDataArray addObject:curArray];
//            }
//            if (self.dataArray.count <= 0 ) {
//                self.noDataImageView.image = [UIImage imageNamed:@"noData.png"];
//                self.noDataImageView.hidden = NO;
//                self.noDataLabel.text = @"还没有课程";
//                self.noDataLabel.hidden = NO;
//            }else{
//                self.noDataImageView.hidden = YES;
//                self.noDataLabel.hidden = YES;
//            }
////            if (self.pageNo < [response[@"courseList"][@"totalRow"] intValue])
////            {
////                self.isMorePage = YES;
////            }
////            else
////            {
////                self.isMorePage = NO;
////            }
//
//            if (self.type == 1)
//            {
//                [self.tableView.mj_header endRefreshing];
//            }
//            else if (self.type == 2)
//            {
//                [self.tableView.mj_footer endRefreshing];
//            }
//            [self.tableView reloadData];
//        }
//    }];
//}
//- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
//    return 1;
//}
//- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if (section) {
////       return [self.classDataArray[section-1] count];
//        return 0;
//    }
//    return self.liveDataArray.count;
//}
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section) {
//        ClassRoomTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ClassRoomCell" forIndexPath:indexPath];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.classRoomInfo = self.classDataArray[indexPath.section - 1][indexPath.row];
//        return cell;
//    }
//    LiveClassRoomTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LiveClassRoomCell" forIndexPath:indexPath];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.classRoomInfo = self.liveDataArray[indexPath.row];
//    return cell;
//}
//
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section) {
//        return 100;
//    }
//    return 300;
//}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section) {
//        ClassRoomDetailViewController *classRoomDetailVC = [[ClassRoomDetailViewController alloc]init];
//        classRoomDetailVC.classRoomInfo = self.classDataArray[indexPath.section - 1][indexPath.row];
//        [self.navigationController pushViewController:classRoomDetailVC animated:YES];
//        return;
//    }
//    LiveDetailViewController *liveDetailVC = [[LiveDetailViewController alloc]init];
//    liveDetailVC.classRoomInfo = self.liveDataArray[indexPath.row];
//    [self.navigationController pushViewController:liveDetailVC animated:YES];
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *view = [[UIView alloc]init];
//    view.frame = CGRectMake(0, 0, kScreenW, 44);
//    view.backgroundColor = [UIColor whiteColor];
//
//    if (!section) {
//        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 12, kScreenW - 40, 20)];
//        label.text = @"直播课程";
//        [view addSubview:label];
//
//        UIButton *moreButton = [[UIButton alloc]initWithFrame:CGRectMake(kScreenW - 60, 7, 60, 30)];
//        [moreButton setTitle:@"更多" forState:UIControlStateNormal];
//        [moreButton setTitleColor:KTEXTMAINCOLOR forState:UIControlStateNormal];
//        moreButton.titleLabel.font = [UIFont systemFontOfSize:14];
//        [moreButton addTarget:self action:@selector(moreLiveList) forControlEvents:UIControlEventTouchUpInside];
//        [view addSubview:moreButton];
//    }else{
//        YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
//        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
//        [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
//        [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppCourseGetSubjectList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
//            if (isuccess) {
//                [self.subjectList removeAllObjects];
//                NSArray *array = [NSArray arrayWithArray:response];
//                for (int i = 0; i < array.count; i++) {
//                    [self.subjectList addObject:array[i][@"name"]];
//                }
//                ChoiceView *choiceview=[[ChoiceView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 41) andtitleArray:self.subjectList];
//                [view addSubview:choiceview];
//                self.choiceView=choiceview;
//
//            }
//        }];
//
//    }
//
//    return view;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 44.01f;
//}
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    UIView *view = [[UIView alloc]init];
//    view.frame = CGRectMake(0, 0, kScreenW, 10);
//    view.backgroundColor = KBACKGROUNDCOLOR;
//    if (section == self.tableView.numberOfSections - 1) {
//        view.frame = CGRectMake(0, 0, kScreenW, 0.01);
//    }
//    return view;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    if (section == self.tableView.numberOfSections-1) {
//        return 0.01f;
//    }
//    return 10.01f;
//}
//#pragma mark - 更多直播
//- (void)moreLiveList{
//    LiveListViewController *liveListVC = [[LiveListViewController alloc]init];
//    [self.navigationController pushViewController:liveListVC animated:YES];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
